
const thinkEndStr = '<!-- think --></div>\n\n';
const stringEmpty = ''

function formatAIResult(content, type = null) {
    content = content.toString()
    if (content.indexOf(thinkEndStr) > -1) {
        content = content.split(thinkEndStr)[1]
    }
    // 代码块提取
    content = extractCodeBlock(content, type)
    if (content == content) {
        content = content.replaceAll("```css", stringEmpty);
        content = content.replaceAll("```javascript", stringEmpty);
        content = content.replaceAll("```markdown", stringEmpty);
        content = content.replaceAll("```json", stringEmpty);
        content = content.replaceAll("```html", stringEmpty);
        content = content.replaceAll("```", stringEmpty);
    }
    return content;
}
function extractCodeBlock(text, type = null) {
    // 检查输入是否为有效字符串
    if (typeof text !== 'string') {
        return text;
    }

    // 正则表达式匹配所有代码块，支持各种类型
    // 格式为 ```[类型](内容)```，类型可选
    let regex = /```(?:\w+)?([\s\S]*?)```/g;
    let matches = [];
    let match;

    // 收集所有匹配的代码块内容
    while ((match = regex.exec(text)) !== null) {
        if (match[1]) {
            matches.push(match[1].trim());
        }
    }

    // 只有当存在且仅存在一个代码块时才返回提取的内容
    if (matches.length === 1) {
        return matches[0];
    } else if (type) {
        regex = new RegExp(`\`\`\`${type}([\\s\\S]*?)\`\`\``, 'g');
        matches = []
        // 收集所有匹配的代码块内容
        while ((match = regex.exec(text)) !== null) {
            if (match[1]) {
                matches.push(match[1].trim());
            }
        }
        if (matches.length === 1) {
            return matches[0];
        }
    }

    // 否则返回原始文本
    return text;
}

function filterDBFiles(filesStr) {
    var files = JSON.parse(filesStr || []) || [];
    return files.filter(_ => _.path.endsWith('.db'))
}

function filterModuleFiles(filesStr) {
    var files = JSON.parse(filesStr || []) || [];
    return files.filter(_ => _.path.endsWith('.js') && _.path.startsWith('pages/') == false)
}


function filterClientLibFiles(filesStr) {
    var files = JSON.parse(filesStr || []) || [];
    var res = files.filter(_ =>
        (_.path.endsWith('.css') || _.path.endsWith('.js'))
        && _.path.indexOf('/components/') < 0
        && _.path.startsWith('pages/'))
    return res
}


function filterClientComponentsFiles(filesStr) {
    var files = JSON.parse(filesStr || []) || [];
    return files.filter(_ =>
        (_.path.endsWith('.css') || _.path.endsWith('.js'))
        && _.path.indexOf('/components/') >= -1)
}

function filterHtmlFiles(filesStr) {
    var files = JSON.parse(filesStr || []) || [];
    return files.filter(_ =>
        _.path.endsWith('.html') && _.path.startsWith('pages/'))
}

function containsPRD(filesStr) {
    var files = JSON.parse(filesStr || []) || [];
    return files.filter(_ =>
        _.path.endsWith('PROJECT.prd')).length > 0
}

function containsReadme(filesStr) {
    var files = JSON.parse(filesStr || []) || [];
    return files.filter(_ =>
        _.path.endsWith('README.md')).length > 0
}


export {
    formatAIResult,
    filterDBFiles,
    filterModuleFiles,
    filterClientLibFiles,
    filterClientComponentsFiles,
    filterHtmlFiles,
    containsPRD,
    containsReadme
}
